package org.darcy.controller.admin.site;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.darcy.entity.BizComment;
import org.darcy.framework.annotation.BussinessLog;
import org.darcy.framework.blog.business.enums.ResponseStatus;
import org.darcy.framework.blog.business.enums.TemplateKeyEnum;
import org.darcy.framework.blog.exception.BlogCommentException;
import org.darcy.framework.model.PageResult;
import org.darcy.framework.model.ResponseVO;
import org.darcy.framework.util.ResultUtil;
import org.darcy.model.BizCommentModel;
import org.darcy.service.BizCommentService;
import org.darcy.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 评论管理
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

	@Autowired
	private BizCommentService commentService;

	@Autowired
	private MailService mailService;

	@RequiresPermissions("comments")
	@PostMapping("/list")
	public PageResult list(BizCommentModel vo) {
		IPage<BizCommentModel> pageInfo = commentService.query(vo);
		return ResultUtil.tablePage(pageInfo);
	}

	@RequiresPermissions("comment:reply")
	@PostMapping(value = "/reply")
	@BussinessLog("回复评论")
	public ResponseVO reply(BizComment comment) {
		try {
			commentService.commentForAdmin(comment);
		} catch (BlogCommentException e) {
			return ResultUtil.error(e.getMessage());
		}
		return ResultUtil.success("成功");
	}

	@RequiresPermissions(value = { "comment:batchDelete", "comment:delete" }, logical = Logical.OR)
	@PostMapping(value = "/remove")
	@BussinessLog("删除评论[{1}]")
	public ResponseVO remove(Long[] ids) {
		if (null == ids) {
			return ResultUtil.error(500, "请至少选择一条记录");
		}
		for (Long id : ids) {
			commentService.delete(id);
		}
		return ResultUtil.success("成功删除 [" + ids.length + "] 条评论");
	}

	@RequiresPermissions("comments")
	@PostMapping("/get/{id}")
	@BussinessLog("获取评论[{1}]详情")
	public ResponseVO get(@PathVariable Long id) {
		return ResultUtil.success(null, this.commentService.getById(id));
	}

	@RequiresPermissions("comments")
	@PostMapping("/edit")
	@BussinessLog("编辑评论")
	public ResponseVO edit(BizComment comment) {
		try {
			commentService.update(comment);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("评论修改失败！");
		}
		return ResultUtil.success(ResponseStatus.SUCCESS);
	}

	@RequiresPermissions("comment:audit")
	@PostMapping("/audit")
	@BussinessLog("审核评论")
	public ResponseVO audit(BizComment comment, String contentText, Boolean sendEmail) {
		try {
			commentService.update(comment);
			if (!StringUtils.isEmpty(contentText)) {
				comment.setContent(contentText);
				commentService.commentForAdmin(comment);
			}
			if (null != sendEmail && sendEmail) {
				BizComment commentDB = commentService.getById(comment.getId());
				mailService.send(commentDB, TemplateKeyEnum.TM_COMMENT_AUDIT, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("评论审核失败！");
		}
		return ResultUtil.success(ResponseStatus.SUCCESS);
	}

	/**
	 * 正在审核的
	 *
	 * @param comment
	 * @return
	 */
	@RequiresUser
	@PostMapping("/listVerifying")
	public ResponseVO listVerifying(BizComment comment) {
		return ResultUtil.success(null, commentService.listVerifying(10));
	}

}
